#!/bin/bash

# Docker构建脚本
# 作者: auth-cloud
# 日期: 2024-01-01

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 项目根目录
PROJECT_ROOT=$(cd "$(dirname "$0")/.." && pwd)
echo -e "${GREEN}项目根目录: ${PROJECT_ROOT}${NC}"

# 检查Docker环境
check_docker() {
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}错误: 未找到Docker环境，请先安装Docker${NC}"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${RED}错误: 未找到Docker Compose环境，请先安装Docker Compose${NC}"
        exit 1
    fi
    
    echo -e "${GREEN}Docker环境检查通过${NC}"
}

# 编译项目
build_project() {
    echo -e "${YELLOW}开始编译项目...${NC}"
    cd "$PROJECT_ROOT"
    
    if mvn clean package -DskipTests; then
        echo -e "${GREEN}项目编译成功${NC}"
    else
        echo -e "${RED}项目编译失败${NC}"
        exit 1
    fi
}

# 构建Docker镜像
build_images() {
    echo -e "${YELLOW}开始构建Docker镜像...${NC}"
    
    # 构建网关服务镜像
    echo -e "${YELLOW}构建网关服务镜像...${NC}"
    docker build -f Dockerfile.gateway -t auth-cloud/gateway:1.0.0 .
    
    # 构建注册中心服务镜像
    echo -e "${YELLOW}构建注册中心服务镜像...${NC}"
    docker build -f Dockerfile.registry -t auth-cloud/registry:1.0.0 .
    
    # 构建认证服务镜像
    echo -e "${YELLOW}构建认证服务镜像...${NC}"
    docker build -f Dockerfile.auth-service -t auth-cloud/auth-service:1.0.0 .
    
    # 构建用户服务镜像
    echo -e "${YELLOW}构建用户服务镜像...${NC}"
    docker build -f Dockerfile.user-service -t auth-cloud/user-service:1.0.0 .
    
    echo -e "${GREEN}所有Docker镜像构建完成${NC}"
}

# 启动开发环境
start_dev() {
    echo -e "${YELLOW}启动开发环境...${NC}"
    cd "$PROJECT_ROOT"
    docker-compose -f docker-compose.dev.yml up -d
    echo -e "${GREEN}开发环境启动完成${NC}"
}

# 停止开发环境
stop_dev() {
    echo -e "${YELLOW}停止开发环境...${NC}"
    cd "$PROJECT_ROOT"
    docker-compose -f docker-compose.dev.yml down
    echo -e "${GREEN}开发环境已停止${NC}"
}

# 启动生产环境
start_prod() {
    echo -e "${YELLOW}启动生产环境...${NC}"
    cd "$PROJECT_ROOT"
    docker-compose up -d
    echo -e "${GREEN}生产环境启动完成${NC}"
}

# 停止生产环境
stop_prod() {
    echo -e "${YELLOW}停止生产环境...${NC}"
    cd "$PROJECT_ROOT"
    docker-compose down
    echo -e "${GREEN}生产环境已停止${NC}"
}

# 查看服务状态
status() {
    echo -e "${YELLOW}服务状态:${NC}"
    docker-compose ps
}

# 查看日志
logs() {
    local service=$1
    if [ -z "$service" ]; then
        echo -e "${YELLOW}所有服务日志:${NC}"
        docker-compose logs -f
    else
        echo -e "${YELLOW}服务 ${service} 日志:${NC}"
        docker-compose logs -f "$service"
    fi
}

# 清理资源
clean() {
    echo -e "${YELLOW}清理Docker资源...${NC}"
    
    # 停止并删除容器
    docker-compose down
    
    # 删除镜像
    docker rmi auth-cloud/gateway:1.0.0 2>/dev/null || true
    docker rmi auth-cloud/registry:1.0.0 2>/dev/null || true
    docker rmi auth-cloud/auth-service:1.0.0 2>/dev/null || true
    docker rmi auth-cloud/user-service:1.0.0 2>/dev/null || true
    
    # 清理未使用的资源
    docker system prune -f
    
    echo -e "${GREEN}Docker资源清理完成${NC}"
}

# 显示帮助信息
show_help() {
    echo "用法: $0 [命令]"
    echo ""
    echo "命令:"
    echo "  build         编译项目并构建Docker镜像"
    echo "  start-dev     启动开发环境 (仅基础服务)"
    echo "  stop-dev      停止开发环境"
    echo "  start-prod    启动生产环境 (所有服务)"
    echo "  stop-prod     停止生产环境"
    echo "  restart-prod  重启生产环境"
    echo "  status        查看服务状态"
    echo "  logs [服务名] 查看服务日志"
    echo "  clean         清理Docker资源"
    echo "  help          显示帮助信息"
    echo ""
    echo "服务名:"
    echo "  mysql         MySQL数据库"
    echo "  redis         Redis缓存"
    echo "  nacos         Nacos注册中心"
    echo "  gateway       网关服务"
    echo "  registry      注册中心服务"
    echo "  auth-service  认证服务"
    echo "  user-service  用户服务"
}

# 主函数
main() {
    case "$1" in
        "build")
            check_docker
            build_project
            build_images
            ;;
        "start-dev")
            check_docker
            start_dev
            ;;
        "stop-dev")
            stop_dev
            ;;
        "start-prod")
            check_docker
            start_prod
            ;;
        "stop-prod")
            stop_prod
            ;;
        "restart-prod")
            stop_prod
            sleep 5
            start_prod
            ;;
        "status")
            status
            ;;
        "logs")
            logs "$2"
            ;;
        "clean")
            clean
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            echo -e "${RED}错误: 未知命令: $1${NC}"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@"
